using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sbMembership.Library.Presenters;

namespace sbMembership.Web
{
    public partial class Default : System.Web.UI.Page
    {
        private ListUsersPresenter listUsersPresenter;
        private AddUserPresenter addUserPresenter;
        private AddRolePresenter addRolePresenter;
        private ListRolesPresenter listRolesPresenter;
        private AssignRolesToUserPresenter assignRolesPresenter;
        private EditRolePresenter editRolePresenter;
        private EditUserPresenter editUserPresenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            listUsersPresenter = new ListUsersPresenter(ListUsersView1);
            ListUsersView1.AttachPresenter(listUsersPresenter);
            listUsersPresenter.LaunchAddUserEvent += new EventHandler(HandleLaunchAddUserEvent);
            listUsersPresenter.DeleteUserCompleteEvent += new EventHandler(HandleDeleteUserCompleteEvent);
            listUsersPresenter.LaunchAssignRoleToUserEvent += new EventHandler(HandleAssignRoleEvent);
            listUsersPresenter.LaunchEditUserEvent += new EventHandler(HandleEditUserEvent);

            addUserPresenter = new AddUserPresenter(AddUserView1);
            AddUserView1.AttachPresenter(addUserPresenter);
            addUserPresenter.AddUserCompleteEvent += new EventHandler(HandleAddUserCompleteEvent);

            addRolePresenter = new AddRolePresenter(AddRoleView1);
            AddRoleView1.AttachPresenter(addRolePresenter);
            addRolePresenter.AddRoleCompleteEvent += new EventHandler(HandleAddRoleCompleteEvent);

            listRolesPresenter = new ListRolesPresenter(ListRolesView1);
            ListRolesView1.AttachPresenter(listRolesPresenter);
            listRolesPresenter.LaunchAddRoleEvent += new EventHandler(HandleLaunchAddRoleEvent);
            listRolesPresenter.DeleteRoleCompleteEvent += new EventHandler(HandleDeleteRoleCompleteEvent);
            listRolesPresenter.LaunchEditRoleEvent += new EventHandler(HandleLaunchEditRoleEvent);

            assignRolesPresenter = new AssignRolesToUserPresenter(AssignUserToRoleView1);
            AssignUserToRoleView1.AttachPresenter(assignRolesPresenter);
            assignRolesPresenter.AssignRoleCompleteEvent += new EventHandler(HandleAssignRoleCompleteEvent);

            editRolePresenter = new EditRolePresenter(EditRoleView1);
            EditRoleView1.AttachPresenter(editRolePresenter);
            editRolePresenter.EditRoleCompleteEvent += new EventHandler(HandleEditRoleCompleteEvent);

            editUserPresenter = new EditUserPresenter(EditUserView1);
            EditUserView1.AttachPresenter(editUserPresenter);
            editUserPresenter.EditUserCompleteEvent += new EventHandler(HandleEditUserCompleteEvent);

            listUsersPresenter.InitViewFor(ListUsersPresenter.ActionEnum.none);
            listRolesPresenter.InitViewFor(ListRolesPresenter.ActionEnum.none);
        }

        private void HandleLaunchAddUserEvent(object sender, EventArgs e)
        {
            addUserPresenter.InitView();
            AddUserModalPopupExtender.Show();
        }

        private void HandleAddUserCompleteEvent(object sender, EventArgs e)
        {
            listUsersPresenter.InitViewFor(ListUsersPresenter.ActionEnum.added);
            AddUserModalPopupExtender.Hide();
            MemberGridUpdatePanel.Update();
        }

        private void HandleDeleteUserCompleteEvent(object sender, EventArgs e)
        {
            listUsersPresenter.InitViewFor(ListUsersPresenter.ActionEnum.deleted);
            MemberGridUpdatePanel.Update();
        }

        private void HandleLaunchAddRoleEvent(object sender, EventArgs e)
        {
            addRolePresenter.InitView();
            AddRoleModalPopupExtender.Show();
        }

        private void HandleAddRoleCompleteEvent(object sender, EventArgs e)
        {
            listRolesPresenter.InitViewFor(ListRolesPresenter.ActionEnum.added);
            AddRoleModalPopupExtender.Hide();
            RoleGridUpdatePanel.Update();
        }

        private void HandleDeleteRoleCompleteEvent(object sender, EventArgs e)
        {
            listRolesPresenter.InitViewFor(ListRolesPresenter.ActionEnum.deleted);
            RoleGridUpdatePanel.Update();
        }

        private void HandleAssignRoleEvent(object sender, EventArgs e)
        {
            assignRolesPresenter.InitViewFor(((AssignRoleEventArgs)e).UserName);
            AssignRoleUpdatePanel.Update();
            AssignRoleModalPopupExtender.Show();
        }

        private void HandleAssignRoleCompleteEvent(object sender, EventArgs e)
        {
            AssignRoleUpdatePanel.Update();
        }

        private void HandleLaunchEditRoleEvent(object sender, EventArgs e)
        {
            editRolePresenter.InitViewFor(((EditRoleEventArgs)e).RoleName);
            EditRoleUpdatePanel.Update();
            EditRoleModalPopupExtender.Show();
        }

        private void HandleEditRoleCompleteEvent(object sender, EventArgs e)
        {
            EditRoleUpdatePanel.Update();
        }

        private void HandleEditUserEvent(object sender, EventArgs e)
        {
            editUserPresenter.InitViewForEditing(((EditUserEventArgs)e).UserName, false);
            EditUserUpdatePanel.Update();
            EditUserModalPopupExtender.Show();
        }

        private void HandleEditUserCompleteEvent(object sender, EventArgs e)
        {
            EditUserModalPopupExtender.Hide();
            listUsersPresenter.InitViewFor(ListUsersPresenter.ActionEnum.updated);
            MemberGridUpdatePanel.Update();
        }

    }
}